/*
 *  Copyright (C) 2022-2024. Maximilian Keppeler (https://www.maxkeppeler.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
@file:OptIn(ExperimentalMaterialApi::class, ExperimentalMaterial3Api::class)

package com.fhc.view.dialog

import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.ModalBottomSheetLayout
import androidx.compose.material.ModalBottomSheetState
import androidx.compose.material.ModalBottomSheetValue
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.MutableIntState
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import com.fhc.view.R
import com.maxkeppeker.sheets.core.models.base.IconSource
import com.maxkeppeker.sheets.core.models.base.rememberUseCaseState
import com.maxkeppeler.sheets.calendar.CalendarView
import com.maxkeppeler.sheets.calendar.models.CalendarConfig
import com.maxkeppeler.sheets.calendar.models.CalendarSelection
import com.maxkeppeler.sheets.calendar.models.CalendarStyle
import com.maxkeppeler.sheets.list.ListView
import com.maxkeppeler.sheets.list.models.ListOption
import com.maxkeppeler.sheets.list.models.ListSelection
import kotlinx.coroutines.launch



val options = listOf(
    ListOption(
        IconSource(R.drawable.ic_fruit_apple),
        titleText = "Apple"
    ),
    ListOption(
        IconSource(R.drawable.ic_fruit_watermelon),
        titleText = "Watermelon",
    ),
    ListOption(
        IconSource(R.drawable.ic_fruit_grapes),
        titleText = "Grapes",
        selected = true
    ),
    ListOption(
        IconSource(R.drawable.ic_fruit_pineapple),
        titleText = "Pineapple",
    ),
)


@Composable
fun BottomSheetSample(
    modifier: Modifier = Modifier,
    modalTypeState: MutableIntState,
    bottomSheetState: ModalBottomSheetState,
    selectionCalendar: CalendarSelection = CalendarSelection.Dates {},
    onSelectOption: (Int, ListOption) -> Unit = {index, option ->},
    screenContent: @Composable () -> Unit
) {
    val coroutine = rememberCoroutineScope()
    val hideBottomSheet = { coroutine.launch {ModalBottomSheetValue.Hidden} }
    val useCaseState =
        rememberUseCaseState(visible = true, embedded = false,onCloseRequest = { hideBottomSheet(); })

    LaunchedEffect(bottomSheetState.currentValue) {
        when (bottomSheetState.currentValue) {
            ModalBottomSheetValue.Hidden,
            ModalBottomSheetValue.Expanded -> {
//                useCaseState.invokeReset() // Manually reset internal state if required
            }
            ModalBottomSheetValue.HalfExpanded -> Unit

        }
    }

    ModalBottomSheetLayout(
        content = screenContent,
        sheetState = bottomSheetState,
        sheetContent = {
            Surface(
                modifier = modifier,
                color = MaterialTheme.colorScheme.surface,
            ) {
                if(modalTypeState.intValue == 0){

                    CalendarView(
                        useCaseState = useCaseState,
                        config = CalendarConfig(
                            style = CalendarStyle.MONTH
                        ),
                        selection = selectionCalendar,
                    )

                }else{

                ListView(
                    useCaseState = useCaseState,
                    selection = ListSelection.Single(
                        showRadioButtons = true,
                        options = options
                    ) { index, option ->
                        onSelectOption(index, option)
                    }
                )
                }

            }
        }
    )
}